﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace A14_Ex01_1
{
    public class MinAndMax
    {
        private const int k_ZeroRemainder = 0;
        private const int k_InitFirstNumber = 0;
        private const int k_InitParamWithZero = 0;
        private const int k_EndOfInput = -1;
        private static int m_MinNumber = 0;
        private static int m_MaxNumber = 0;
        private static int m_NumbersCounter = 0;
        private static int m_NumbersSum = 0;
        private static float m_NumbersAvg = 0f;
        private static int m_NumbersDivededWithFirstNumberCounter = 0;
        private static bool v_IsValid = true;
        private static bool v_IsEndCharFound = false;
        private static int m_FirstNumber = 0;
        private static int m_CurrentInputNumber = 0;

        public static void ReadInputNumbersFromUser()
        {
            System.Console.WriteLine(@"Hello! please enter numbers and press enter after each number to finish enter {0}{1}", k_EndOfInput, System.Environment.NewLine);

            while (!v_IsEndCharFound)
            {
                string inputNumbersStr = System.Console.ReadLine();

                validateNumberStr(inputNumbersStr);

                if (v_IsValid)
                {
                    updateMinMaxWithCurrentNewNumber();
                }
                else if (!v_IsValid && !v_IsEndCharFound)
                {
                    System.Console.WriteLine(@"The number was invalid, please enter valid numbers only. to finish enter {0}{1}", k_EndOfInput, System.Environment.NewLine);
                    v_IsValid = true;
                }
            }
        }

        public static void PrintStatistics()
        {
            System.Console.WriteLine(
@"Hello!{0}
The max number is: {1}{0}
The min number is: {2}{0}
You've entered {3} numbers.{0}
The numbers avg is: {4}{0}
The ammount of numbers divided with first number {5} are: {6}{0}", 
System.Environment.NewLine,
m_MaxNumber,
m_MinNumber,
m_NumbersCounter,
m_NumbersAvg,
m_FirstNumber,
m_NumbersDivededWithFirstNumberCounter);
        }

        public static void Finish()
        {
            System.Console.WriteLine(@"Thank you for using min and max program." + System.Environment.NewLine);
        }

        private static void updateMinMaxWithCurrentNewNumber()
        {
            m_NumbersCounter++;
            m_NumbersSum += m_CurrentInputNumber;

            if (m_FirstNumber == k_InitFirstNumber)
            {
                m_FirstNumber = m_MaxNumber = m_MinNumber = m_CurrentInputNumber;
                m_NumbersAvg = (float)m_CurrentInputNumber;
            }
            else
            {
                isNumDivWithFirstNum(m_CurrentInputNumber);
                isNumMinOrMax(m_CurrentInputNumber);
                updateNumbersAvg();
            }
        }

        private static void updateNumbersAvg()
        {
            m_NumbersAvg = (float)m_NumbersSum / m_NumbersCounter;
        }

        private static void isNumMinOrMax(int i_CurrentInputNumber)
        {
            m_MinNumber = Math.Min(m_MinNumber, i_CurrentInputNumber);
            m_MaxNumber = Math.Max(m_MaxNumber, i_CurrentInputNumber);
        }

        private static void isNumDivWithFirstNum(int i_CurrentInputNumber)
        {
            int divResultRemainder;
            Math.DivRem(i_CurrentInputNumber, m_FirstNumber, out divResultRemainder);

            if (Math.Equals(divResultRemainder, k_ZeroRemainder))
            {
                m_NumbersDivededWithFirstNumberCounter++;
            }
        }

        private static void validateNumberStr(string i_InputNumberStr)
        {
            try
            {
                if (int.TryParse(i_InputNumberStr, out m_CurrentInputNumber))
                {
                    if (Math.Equals(m_CurrentInputNumber, k_EndOfInput))
                    {
                        v_IsEndCharFound = true;
                        v_IsValid = false;
                    }
                    else if (m_CurrentInputNumber <= 0)
                    {
                        v_IsValid = false;
                    }
                }
                else
                {
                    v_IsValid = false;
                }
            }
            catch (Exception)
            {
                v_IsValid = false;
            }
        }
    }
}
